﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JOATServer.SocietyFramework.Core;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;

namespace JOATServer
{
    public class NewsAutomaton : Automaton
    {
        protected static readonly string IDLE_STATE = "IDLE_STATE";
        protected static readonly string READ_NEWS_STATE = "READ_NEWS_STATE";
        protected static readonly string SEND_NEWS_STATE = "SEND_NEWS_STATE";

        protected static string readNews;

        public NewsAutomaton()
        {
            currentStateName = IDLE_STATE;

            states.Add(IDLE_STATE, new IdleState(this));
            states[IDLE_STATE].transition = new SimpleTransition(this, READ_NEWS_STATE);

            states.Add(READ_NEWS_STATE, new ReadNewsState(this));
            states[READ_NEWS_STATE].transition = new SimpleTransition(this, SEND_NEWS_STATE);

            states.Add(SEND_NEWS_STATE, new SendNewsState(this));
            states[SEND_NEWS_STATE].transition = new SimpleTransition(this, IDLE_STATE);
        }

        protected class IdleState : State
        {
            public IdleState(BaseAutomaton owner) : base(owner) { }

            public override void StateCode()
            {
                Owner.GetNextMessage();
            }
        }

        protected class ReadNewsState : State
        {
            public ReadNewsState(BaseAutomaton owner) : base(owner) { }

            public override void StateCode()
            {
                NewsAutomaton automaton = (NewsAutomaton)Owner;
                if (automaton.currentMessage != null)
                {
                    StringBuilder sb = new StringBuilder();
                    string currentDescription;
                    using (XmlReader reader = XmlReader.Create(File.OpenRead(@"science_news.xml")))
                    {
                        while (reader.Read())
                        {
                            if (reader.Name == "description")
                            {
                                reader.Read();
                                currentDescription = reader.Value
                                    .Replace("\n", "")
                                    .Replace("'", " ")
                                    .Replace("\"", " ")
                                    .Trim();
                                if (currentDescription != "")
                                {
                                    sb.Append(currentDescription);
                                    sb.Append("|");
                                }
                            }
                        }
                    }
                    sb.Remove(sb.Length - 1, 1);
                    readNews = sb.ToString();
                }
            }
        }

        protected class SendNewsState : State
        {
            public SendNewsState(BaseAutomaton owner) : base(owner) { }

            public override void StateCode()
            {
                MessageRouter.Instance.Send(Owner.currentMessage.From, new Message(Owner.GetType().Name, readNews));
            }
        }
    }
}
